module gui.groupdlg;

import mirdefine, mirshare;
import gui.gwindow, gui.gbutton;

import common.imagearray, common.imagehandler;
import share.container, share.types, share.protocol;

import AGui.AGui;

bool g_fAllowGroup = true;

public class GroupMember
{
    public string MemberName;
}

public class GroupWindow : GWindow, ActionListener
{ 
    public Vector!(GroupMember) GroupList;

    private GButton AllowGroupButton;
    private GButton CreateGroupButton;
    private GButton AddMemberButton;
    private GButton DeleteMemberButton;
    private GButton CloseButton;

    public void actionPerformed(ActionEvent evt)
    {
        GButton button = cast(GButton)evt.getSource();
        if (button !is null)
        {
            if (button is AllowGroupButton)
            {
                g_fAllowGroup = !g_fAllowGroup;
                MySocket.SendGroupMode(g_fAllowGroup);
            }
            
            if (button is CreateGroupButton)
            {
                MySocket.SendGroupMessage(CM_CREATEGROUP, "mandul");
            }
            
            if (button is AddMemberButton)
            {
                MySocket.SendGroupMessage(CM_ADDGROUPMEMBER, "mandul");
            }
            
            if (button is DeleteMemberButton)
            {
                MySocket.SendGroupMessage(CM_DELGROUPMEMBER, "mandul");
            }
            
            if (button is CloseButton)
            {
                GroupWindow.hide();
            }
        }
    }

    
    public this()
    {
        AllowGroupButton = new GButton;
        AllowGroupButton.addActionListener(this);
        
        CreateGroupButton = new GButton;
        CreateGroupButton.addActionListener(this);
        
        AddMemberButton = new GButton;
        AddMemberButton.addActionListener(this);
        
        DeleteMemberButton = new GButton;
        DeleteMemberButton.addActionListener(this);

        CloseButton = new GButton;
        CloseButton.addActionListener(this);

        this.add(AllowGroupButton);
        this.add(CreateGroupButton);
        this.add(AddMemberButton);
        this.add(DeleteMemberButton);
        this.add(CloseButton);
        
        guiMan.add(this);
    }
    
    public ~this()
    {
        guiMan.remove(this);

        delete AllowGroupButton;
        delete CreateGroupButton;
        delete AddMemberButton;
        delete DeleteMemberButton;
        delete CloseButton;
    }

    public void createGroupWindow(ImageArray iarr)
    {
        immutable color = Color(255, 255, 255, 255);

        super.createWindow(iarr, _WNDIMGIDX_GROUP, true, true, 258, 241);
        this.setFontColor(color);

        AllowGroupButton.createButton(iarr, 1370, 1371, 22, 44, false); //19, 55
        AllowGroupButton.setVisibility(true);

        CreateGroupButton.createButton(iarr, 1362, 1363, 38, 189, false);
        CreateGroupButton.setVisibility(true);

        AddMemberButton.createButton(iarr, 1365, 1366, 98, 189, false);
        AddMemberButton.setVisibility(true);

        DeleteMemberButton.createButton(iarr, 1368, 1369, 158, 189, false);
        DeleteMemberButton.setVisibility(true);

        CloseButton.createButton(iarr, 1221, 1222, 215, 196, false);
        CloseButton.setVisibility(true);
    }

    protected override void paintComponent(PaintEvent paintEvent)
    {
        int nX = 50, nY = 90;
        
        paintEvent.graphics().drawText(AGui.Types.IntPoint(50, 15), g_szUserID,
                                       getFontColor(), getFont());
        
        foreach (i, GroupMember member; GroupList)
        {
            paintEvent.graphics().drawText(AGui.Types.IntPoint(nX, nY), member.MemberName,
                                           getFontColor(), getFont());
            
            if ((i % 2) == 0) 
                nX += 100;
            else
            {
                nX -= 100;
                nY += 50;
            }
        }
    }
}